package com.tedu.egweiboall.controller;

import com.tedu.egweiboall.mapper.CommentMapper;
import com.tedu.egweiboall.pojo.dto.CommentDTO;
import com.tedu.egweiboall.pojo.entity.Comment;
import com.tedu.egweiboall.pojo.entity.User;
import com.tedu.egweiboall.result.JsonResult;
import com.tedu.egweiboall.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@Api(tags = "评论模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @PostMapping("/v1/comment/insert")
    @ApiOperation("发表评论")
    public JsonResult insert(CommentDTO commentDTO, HttpSession session){
        User user =(User)session.getAttribute("User");
        if (user==null){
            return new JsonResult(Status.NOT_LOGIN);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUserId(user.getId());
        commentMapper.insert(comment);
        return new JsonResult(Status.OPERATION_SUCCESS);
    }

    @GetMapping("/v1/comment/selectByweiboId")
    @ApiOperation("查看评论")
    public JsonResult selectByweiboId(Integer id){
        return new JsonResult(Status.OPERATION_SUCCESS,commentMapper.selectByweiboId(id));
    }
}
